<template>
    <div>
        <!-- 条件列表 -->
        <div style="text-align:left" v-if="flg.searchFlg">
            <el-form :inline="true" :model="queryCondition" class="demo-form-inline">
                <el-form-item label="登录地址" style="font-weight: bold">
                    <el-input v-model="queryCondition.ipaddr" placeholder="请输入登录地址" size="mini"></el-input>
                </el-form-item>
                <el-form-item label="用户名称" style="font-weight: bold">
                    <el-input v-model="queryCondition.username" placeholder="请输入用户名称" size="mini"></el-input>
                </el-form-item>

                <el-form-item label="状态" style="font-weight: bold">
                    <el-select v-model="queryCondition.status" clearable prop="isDelete" size="mini">
                        <el-option label="成功" value="0"></el-option>
                        <el-option label="失败" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="操作时间" style="font-weight: bold">
                    <el-date-picker v-model="rangeTime" type="daterange" size="mini" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd HH:mm:ss"
                        :default-time="['00:00:00', '23:59:59']">
                    </el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" size="mini" @click="getList()">查询</el-button>
                </el-form-item>
            </el-form>
        </div>

        <!-- 操作列表 -->
        <el-row style="margin-bottom:10px">
            <el-col :span="12" style="text-align:left">
                <el-button type="danger" icon="el-icon-delete" size="mini" plain @click="handleDelete">删除</el-button>
                <el-button type="danger" icon="el-icon-minus" size="mini" plain @click="handleclear">清空</el-button>

                <el-button type="danger" icon="el-icon-minus" size="mini" plain @click="handleCacheclear">清空缓存</el-button>
            </el-col>
            <el-col :span="12" style="text-align:right">
                <el-tooltip class="item" effect="dark" :content="flg.searchFlg ? '隐藏搜索' : '展开搜索'" placement="top">
                    <el-button icon="el-icon-search" size="mini" circle @click="flg.searchFlg = !flg.searchFlg"></el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="刷新" placement="top">
                    <el-button icon="el-icon-refresh" size="mini" circle @click="getList"></el-button>
                </el-tooltip>
            </el-col>
        </el-row>
        <!-- 操作列表 -->

        <!-- 条件列表 -->
        <el-table :data="list" stripe style="width: 100%" @selection-change="handleSelectionChange"
            v-loading="flg.loadingFlg">
            <el-table-column type="selection" width="45" />
            <el-table-column label="访问编码" prop="infoId" />
            <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" />
            
            <el-table-column label="登录地址" align="center" prop="ipaddr" :show-overflow-tooltip="true" />
            <el-table-column label="登录地点" align="center" prop="loginLocation" :show-overflow-tooltip="true" />
            <el-table-column label="浏览器" align="center" prop="browser" :show-overflow-tooltip="true" />
            <el-table-column label="操作系统" align="center" prop="os" :show-overflow-tooltip="true" />

            <el-table-column label="登录状态" prop="businessType">
                <template slot-scope="scope">
                    <el-tag v-show="scope.row.status == 0" type="success">成功</el-tag>
                    <el-tag v-show="scope.row.status == 1" type="danger">失败</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作信息" align="center" prop="msg" :show-overflow-tooltip="true" />

            <el-table-column label="操作日期" prop="operTime">
                <template slot-scope="scope">
                    <span>{{ formatDate(scope.row.loginTime) }}</span>
                </template>
            </el-table-column>
        </el-table>
        <!--    分页框-->
        <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryCondition.pageSize" :page-sizes="[10, 20, 50, 100, 10000]"
                :page-size="queryCondition.pageNum" layout="total, sizes, prev, pager, next, jumper"
                :total="queryCondition.pageTotal">
            </el-pagination>
        </div>
        <!--    分页框-->

        
    </div>
</template>
<script>
import { list, deleteLogininfor, clear, clearCache } from "@/js/api/monitor/logininfor"
import alter from '@/js/axios'
export default {
    name: "operlogIndex",
    data() {
        return {
            list: [],
            multipleSelectionId: [],//多选的id
            queryCondition: {
                pageSize: 1,//查询第几页
                pageNum: 10,//一页显示多少行
                ipaddr: "",
                username: "",
                status: "",
                srartTime: "",
                endTime: ""
            },
            rangeTime: [],
            flg: {
                loadingFlg: true,
                searchFlg: true,
                batchDeleteFlg: true
            },
        }
    },
    created() {
        //加载用户列表
        this.getList()
    },
    methods: {
        handleSizeChange(val) {
            //  修改一页显示条数
            this.queryCondition.pageNum = val
            this.getList()
        },
        handleCurrentChange(val) {
            //修改页数
            this.queryCondition.pageSize = val
            this.getList()
        },
        //处理多选
        handleSelectionChange(val) {
            this.multipleSelectionId = []
            val.forEach(element => {
                this.multipleSelectionId.push(element.infoId    )
            });

        },
        //清空日志
        handleclear() {
            this.$confirm('是否清空日志?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.flg.loadingFlg = true
                clear().then(() => {
                    this.getList()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        //删除数据
        handleDelete() {
            this.$confirm('是否删除数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                if (this.multipleSelectionId.length == 0) {
                    alter.errorMessage("未选择数据！")
                    return
                }
                let params = {
                    ids: this.multipleSelectionId.toString()
                }
                this.flg.loadingFlg = true
                deleteLogininfor(params).then(() => {
                    this.getList()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        //清空日志缓存
        handleCacheclear() {
            clearCache().then(()=>{
                this.getList()
            })
        },
        //获取日志列表信息
        getList() {
            this.flg.loadingFlg = true

            let params = {
                pageSize: this.queryCondition.pageSize,//查询第几页
                pageNum: this.queryCondition.pageNum,//一页显示多少行
                ipaddr: this.queryCondition.ipaddr,
                username: this.queryCondition.username,
                status: this.queryCondition.status,
                startTime: this.rangeTime[0] == undefined ? null : this.rangeTime[0],
                endTime: this.rangeTime[1] == undefined ? null : this.rangeTime[1],
            }

            list(params).then(res => {
                this.list = res.data.data.list
                this.queryCondition.pageTotal = res.data.data.size
                this.flg.loadingFlg = false
            })

        },

    }
}
</script>
<style></style>